﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;

namespace Uranus.Common
{
    public class MailCommon
    {
        /// <summary>
        /// 发件人地址
        /// </summary>
        public string FromAddress { get; set; }
        /// <summary>
        /// 发件人姓名
        /// </summary>
        public string FromName { get; set; }
        /// <summary>
        /// 发件人登录名
        /// </summary>
        public string FromLoginName { get; set; }
        /// <summary>
        /// 发件人登陆密码
        /// </summary>
        public string FromPwd { get; set; }
        /// <summary>
        /// 发件smtp服务器地址
        /// </summary>
        public string smtpAddress { get; set; }
        /// <summary>
        /// 发送普通邮件
        /// </summary>
        /// <param name="Title"> 邮件标题</param>
        /// <param name="Body"> 邮件正文</param>
        /// <param name="ToAddress"> 接收地址</param>
        public void NormailMail(string Title, string Body, params string[] ToAddress)
        {
            try
            {
                MailMessage mail = new MailMessage();
                mail.Subject = Title;
                mail.SubjectEncoding = Encoding.UTF8;
                mail.Body = Body;
                mail.BodyEncoding = Encoding.UTF8;
                mail.From = new MailAddress(FromAddress, FromName);
                for (int i = 0; i < ToAddress.Count(); i++)
                {
                    mail.To.Add(ToAddress[i]);
                }
                SmtpClient smtp = new SmtpClient(smtpAddress, 25);
                smtp.Credentials = new NetworkCredential(FromLoginName, FromPwd);
                smtp.Send(mail);
                mail.Dispose();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 发送Html邮件
        /// </summary>
        /// <param name="Title"> 标题</param>
        /// <param name="HtmlBody"> Html正文</param>
        /// <param name="ToAddress"> 接收地址</param>
        public void NormailAddHtmlmail(string Title, string HtmlBody, params string[] ToAddress)
        {
            try
            {
                MailMessage mail = new MailMessage();
                mail.Subject = Title;
                mail.SubjectEncoding = Encoding.UTF8;
                mail.Body = HtmlBody;
                mail.IsBodyHtml = true;
                mail.BodyEncoding = Encoding.UTF8;
                mail.From = new MailAddress(FromAddress, FromName);
                for (int i = 0; i < ToAddress.Count(); i++)
                {
                    mail.To.Add(ToAddress[i]);
                }
                SmtpClient smtp = new SmtpClient(smtpAddress, 25);
                smtp.Credentials = new NetworkCredential(FromLoginName, FromPwd);
                smtp.Send(mail);
                mail.Dispose();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 发送带Html的邮件
        /// </summary>
        /// <param name="Title"> 标题</param>
        /// <param name="Body"> 正文</param>
        /// <param name="Html"> Html正文部分 </param>
        /// <param name="ToAddress"> 接收地址</param>
        public void NormailAddHtmlmail(string Title, string Body, string Html, params string[] ToAddress)
        {
            try
            {
                MailMessage mail = new MailMessage();
                mail.Subject = Title;
                mail.SubjectEncoding = Encoding.UTF8;
                mail.Body = Body;
                AlternateView htmlview = AlternateView.CreateAlternateViewFromString(Html, Encoding.UTF8, "text/html");
                mail.BodyEncoding = Encoding.UTF8;
                mail.AlternateViews.Add(htmlview);
                mail.From = new MailAddress(FromAddress, FromName);
                for (int i = 0; i < ToAddress.Count(); i++)
                {
                    mail.To.Add(ToAddress[i]);
                }
                SmtpClient smtp = new SmtpClient(smtpAddress, 25);
                smtp.Credentials = new NetworkCredential(FromLoginName, FromPwd);
                smtp.Send(mail);
                mail.Dispose();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 发送包含图片的邮件
        /// </summary>
        /// <param name="Title"> 标题</param>
        /// <param name="Body"> 正文</param>
        /// <param name="Html"> Html正文</param>
        /// <param name="Pictab"> 图片标识</param>
        /// <param name="PicPath"> 图片路径</param>
        /// <param name="ToAddress"> 收件人</param>
        public void PictureMail(string Title, string Body, string Html, string[] Pictab, string[] PicPath, params string[] ToAddress)
        {
            try
            {
                if (PicPath.Count() != Pictab.Count())
                {
                    throw new Exception("图片个数与标识不一致");
                }
                MailMessage mail = new MailMessage();
                mail.Subject = Title;
                mail.SubjectEncoding = Encoding.UTF8;
                mail.Body = Body;
                AlternateView htmlview = AlternateView.CreateAlternateViewFromString(Html, Encoding.UTF8, "text/html");
                mail.BodyEncoding = Encoding.UTF8;
                for (int i = 0; i < Pictab.Count(); i++)
                {
                    LinkedResource lr = new LinkedResource(PicPath[i], "image/gif");
                    lr.ContentId = Pictab[i];
                    htmlview.LinkedResources.Add(lr);
                }
                mail.AlternateViews.Add(htmlview);
                mail.From = new MailAddress(FromAddress, FromName);
                for (int i = 0; i < ToAddress.Count(); i++)
                {
                    mail.To.Add(ToAddress[i]);
                }
                SmtpClient smtp = new SmtpClient(smtpAddress, 25);
                smtp.Credentials = new NetworkCredential(FromLoginName, FromPwd);
                smtp.Send(mail);
                mail.Dispose();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 发送包含附件的普通邮件
        /// </summary>
        /// <param name="Title"> 标题</param>
        /// <param name="Body"> 正文</param>
        /// <param name="AffixPath"> 附件地址</param>
        /// <param name="ToAddress"> 收件人地址 </param>
        public void AffixHtml(string Title, string Body, string[] AffixPath, params string[] ToAddress)
        {
            try
            {
                MailMessage mail = new MailMessage();
                mail.Subject = Title;
                mail.SubjectEncoding = Encoding.UTF8;
                mail.Body = Body;
                mail.BodyEncoding = Encoding.UTF8;

                for (int i = 0; i < AffixPath.Count(); i++)
                {
                    Attachment a = new Attachment(AffixPath[i]);
                    mail.Attachments.Add(a);
                }
                mail.From = new MailAddress(FromAddress, FromName);
                for (int i = 0; i < ToAddress.Count(); i++)
                {
                    mail.To.Add(ToAddress[i]);
                }
                SmtpClient smtp = new SmtpClient(smtpAddress, 25);
                smtp.Credentials = new NetworkCredential(FromLoginName, FromPwd);
                smtp.Send(mail);
                mail.Dispose();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 发送包含附件的Html普通邮件
        /// </summary>
        /// <param name="Title"> 标题</param>
        /// <param name="Body"> 正文</param>
        /// <param name="Html"> Html正文</param>
        /// <param name="AffixPath"> 附件地址</param>
        /// <param name="ToAddress"> 收件人地址 </param>
        public void AffixHtml(string Title, string Body, string Html, string[] AffixPath, params string[] ToAddress)
        {
            try
            {
                MailMessage mail = new MailMessage();
                mail.Subject = Title;
                mail.SubjectEncoding = Encoding.UTF8;
                mail.Body = Body;
                AlternateView htmlview = AlternateView.CreateAlternateViewFromString(Html, Encoding.UTF8, "text/html");
                mail.BodyEncoding = Encoding.UTF8;
                mail.AlternateViews.Add(htmlview);
                for (int i = 0; i < AffixPath.Count(); i++)
                {
                    Attachment a = new Attachment(AffixPath[i]);
                    mail.Attachments.Add(a);
                }
                mail.From = new MailAddress(FromAddress, FromName);
                for (int i = 0; i < ToAddress.Count(); i++)
                {
                    mail.To.Add(ToAddress[i]);
                }
                SmtpClient smtp = new SmtpClient(smtpAddress, 25);
                smtp.Credentials = new NetworkCredential(FromLoginName, FromPwd);
                smtp.Send(mail);
                mail.Dispose();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 发送包含附件的Html图片邮件
        /// </summary>
        /// <param name="Title"> 标题</param>
        /// <param name="Body"> 正文</param>
        /// <param name="Html"> Html正文</param>
        /// <param name="Pictab"> 图片标识</param>
        /// <param name="PicPath"> 图片地址</param>
        /// <param name="AffixPath"> 附件地址</param>
        /// <param name="ToAddress"> 收件人地址 </param>
        public void AffixHtml(string Title, string Body, string Html, string[] Pictab, string[] PicPath, string[] AffixPath, params string[] ToAddress)
        {
            try
            {
                if (PicPath.Count() != Pictab.Count())
                {
                    throw new Exception("图片个数与标识不一致");
                }
                MailMessage mail = new MailMessage();
                mail.Subject = Title;
                mail.SubjectEncoding = Encoding.UTF8;
                mail.Body = Body;
                AlternateView htmlview = AlternateView.CreateAlternateViewFromString(Html, Encoding.UTF8, "text/html");
                mail.BodyEncoding = Encoding.UTF8;
                for (int i = 0; i < Pictab.Count(); i++)
                {
                    LinkedResource lr = new LinkedResource(PicPath[i], "image/gif");
                    lr.ContentId = Pictab[i];
                    htmlview.LinkedResources.Add(lr);
                }
                mail.AlternateViews.Add(htmlview);
                for (int i = 0; i < AffixPath.Count(); i++)
                {
                    Attachment a = new Attachment(AffixPath[i]);
                    mail.Attachments.Add(a);
                }
                mail.From = new MailAddress(FromAddress, FromName);
                for (int i = 0; i < ToAddress.Count(); i++)
                {
                    mail.To.Add(ToAddress[i]);
                }
                SmtpClient smtp = new SmtpClient(smtpAddress, 25);
                smtp.Credentials = new NetworkCredential(FromLoginName, FromPwd);
                smtp.Send(mail);
                mail.Dispose();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

}
